package com.lt.flashcard.model.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.bson.types.ObjectId;

import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.annotations.Indexed;

/**
 * Simple
 * @author ocsen
 *
 */
@Entity(value="taskqueue", noClassnameStored=true)
public class TaskQueue {
	
	@Id
	private ObjectId id;
	/**
	 * Description
	 */
	private String description;
	/**
	 * The debug information
	 */
	private String info;
	private Date created = new Date();
	/**
	 * The time that start this job
	 */
	private Date started;
	/**
	 * The time finish this job
	 */
	private Date end;
	/**
	 * The status of this job
	 */
	private int status = STATUS_NEW;
	public static final int STATUS_NEW = 0;
	public static final int STATUS_PROCESSING = 1;
	public static final int STATUS_SUCCESS = 2;
	public static final int STATUS_FAIL = 3;
	public static final int STATUS_CANCEL = 4;
	/**
	 * The name of this queue, we have multiple tasks in a queue
	 */
	@Indexed
	private String name;
	/**
	 * Store the param for this task
	 */
	private Map<String, Object> params;
	
	public ObjectId getId() {
		return id;
	}
	
	public void setId(ObjectId id) {
		this.id = id;
	}
	
	public String getInfo() {
		return info;
	}
	
	public void setInfo(String info) {
		this.info = info;
	}
	
	public Date getStarted() {
		return started;
	}
	
	public void setStarted(Date started) {
		this.started = started;
	}
	
	public Date getEnd() {
		return end;
	}
	
	public void setEnd(Date end) {
		this.end = end;
	}
	
	public int getStatus() {
		return status;
	}
	
	public void setStatus(int status) {
		this.status = status;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Map<String, Object> getParams() {
		return params;
	}
	
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	
	public void setParam(String name, Object value) {
		if(params == null)
			params = new HashMap<String, Object>();
		params.put(name, value);
	}
	
	public Object getParam(String name) {
		if(params != null && params.containsKey(name))
			return params.get(name);
		return null;
	}
	
	public String getParamString(String name) {
		Object val = getParam(name);
		if(val == null)
			return null;
		else
			return String.valueOf(val);
	}
	
	public int getParamInt(String name, int defaultvalue) {
		Object val = getParam(name);
		if(val == null)
			return defaultvalue;
		else if(val instanceof Integer)
			return (Integer) val;
		else {
			try {
				return Integer.parseInt(String.valueOf(val));
			}
			catch (Exception e) {
				return defaultvalue;
			}
		}
	}
	
	public Long getParamLong(String name) {
		Object val = getParam(name);
		if(val == null)
			return null;
		else if(val instanceof Long)
			return (Long) val;
		else {
			try {
				return Long.parseLong(String.valueOf(val));
			}
			catch (Exception e) {
				return null;
			}
		}
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the created
	 */
	public Date getCreated() {
		return created;
	}

	/**
	 * @param created the created to set
	 */
	public void setCreated(Date created) {
		this.created = created;
	}
}